//a single inventory object
function InventoryItem(name,execute,frequency,probability,description,display)
{
    this.counter = 0;
    this.name = name;
    this.frequency = frequency;
    this.execute = execute;
    this.probability = probability;
    this.description = description;
    this.display = display
}

function Manager() {

    //A list of inventory events that'll be triggered periodically
    this.inventory = [];

    this.init = function() {
        
    }
    
    /* called at the start of every day */
    this.nextDay = function (day) {

        //logic to iterate through the inventory and trigger events for their 
        //respective frequency
        for (var it in this.inventory) {
            var item = this.inventory[it];
            //console.log(item);
            //checking if the inventory item is a periodic event
            if (item.frequency > 0) {
                if (item.counter == item.frequency) {
                    //reset the counter
                    item.counter = 0;

                    if (item.display) {
                        game.ui.inventory(item, function () {
                            item.execute();
                            game.ui.writeConsole(item.name + ': ' + item.description);
                        });
                    }
                    else{
                        //triggering the execute for that inventory item
                        game.ui.writeConsole(item.name + ': ' + item.description);
                        item.execute();    
                    }                    
                }
                else {
                    item.counter++;
                }
            }
            else {
                var num = Math.random();
                if (num <= item.probability) {
                    if (item.display) {
                        game.ui.inventory(item, function () {
                            game.ui.writeConsole(item.name + ': ' + item.description);
                            item.execute();
                        });
                    } else {
                        item.execute();
                        game.ui.writeConsole(item.name + ': ' + item.description);
                    }
                }
            }
        }

        var currentBuilding = game.logic.upcomingBuildings[0];
        //a condition to check when the building is hit by the character
        if (currentBuilding.hitdate == day) {

            //sending opportunities for the modal
            game.ui.opportunity(currentBuilding.locTag, currentBuilding.opportunities, function (choice) {
                choice.execute();
                game.ui.writeConsole(choice.name + ': ' + choice.outcome);
            });

            //pop and add a new building to the list
            game.logic.updateBuildings();
        }
        if (game.logic.person.happiness >= 100) {
            game.logic.person.happiness = 100;
        }
        game.checkState();
    }

    //Adding to the inventory
    this.addToInventory = function(name,execute,frequency,probability,description,display) {
        var item = new InventoryItem(name,execute,frequency,probability,description,display);
        game.manager.inventory.push(item);        
    }
   
}

